{% extends 'layout/basic.html' %}
{% load static %}

{% block title %}用户短信登录{% endblock %}
{% block css %}
    <link rel="stylesheet" href="{% static 'css/account.css' %}">
    <style>
        .error-msg {
            color: red;
            position: absolute;
            font-size: 13px;
        }
    </style>
{% endblock %}


{% block content %}
    <div class="account">
        <div class="title">用户短信登录</div>
        <form id="smsForm" method="POST" novalidate>
            {% csrf_token %}
            {% for field in form %}
                {% if field.name == 'code' %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        <div class="row">
                            <div class="col-xs-7">
                                {{ field }}
                                <span class="error-msg"></span>
                            </div>
                            <div class="col-xs-5">
                                <input id="btnSms" type="button" class="btn btn-default" value="点击获取验证码">
                            </div>
                        </div>
                    </div>
                {% else %}
                    <div class="form-group">
                        <label for="{{ field.id_for_label }}">{{ field.label }}</label>
                        {{ field }}
                        <span class="error-msg"></span>
                    </div>
                {% endif %}
            {% endfor %}
            <div class="checkbox">
                <div style="float: right;">
                    <a href="{% url 'login' %}">用户名密码登录?</a>
                </div>
            </div>
            <div class="row">
                <div class="col-xs-3">
                    <input id="btnSubmit" type="button" class="btn btn-primary" value="登  录"/>
                </div>
            </div>
        </form>

    </div>
{% endblock %}

{% block js %}
    <script>
        $(function () {
            BindClickBtnSms();
            BindClickSubmit();
        });
        /*点击登录*/
        function BindClickSubmit() {
            $('#btnSubmit').click(function () {
                $('.error-msg').empty();
                //数据发送到后端
                $.ajax({
                    url: '{% url 'login_sms' %}',
                    type: 'POST',
                    data: $('#smsForm').serialize(),//收集表单中的数据，自动携带csrftoken
                    dataType: 'JSON',
                    success: function (res) {
                        if (res.status){
                            location.href = res.data;
                        }else {
                            $.each(res.error,function (key,value) {
                                $('#id_' + key).next().text(value[0]);
                            })
                        }
                    }
                })
            })
        }
        /*点击获取验证码事件绑定*/
        function BindClickBtnSms() {
            $('#btnSms').click(function () {
                //获取用户输入的手机号
                //找到输入框的ID，根据ID获取值，如何找到输入框的ID？
                var mobilePhone = $('#id_mobile_phone').val();
                //发送ajax请求,将手机号发送到后端
                $.ajax({
                    url: '{% url 'send_sms' %}',
                    type: 'GET',
                    dataType: 'JSON',
                    data: {mobile_phone: mobilePhone, tpl: "login"},
                    success: function (res) {
                        //发送成功之后自动执行的函数,res就是后端的返回值
                        if (res.status) {
                            //console.log('发送成功，倒计时')
                            sendSmsRemind();
                        } else {
                            //显示错误信息
                            console.log(res);//{status:False,error:{mobile_phone:['错误信息',]}}
                            $.each(res.error, function (key, value) {
                                $('#id_' + key).next().text(value[0]);
                            })
                        }
                    }
                })

            })
        }
        /*倒计时功能*/
        function sendSmsRemind() {
            var $smsBtn = $('#btnSms');
            $smsBtn.prop('disabled', true);//禁用按钮

            var time = 60;
            var remind = setInterval(function () {
                $smsBtn.val(time + '秒重新发送');
                time = time - 1;
                if (time < 1) {
                    clearInterval(remind);
                    $smsBtn.val('点击获取验证码').prop('disabled', false);
                }
            }, 1000)
        }
    </script>
{% endblock %}